package ${package.Controller};


import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import com.zsq.common.base.BaseResult;
import com.zsq.common.utils.MyUtils;
<#-- 引入服务类 -->
import ${package.Service}.${table.serviceName};
<#-- 引入实体类 -->
import ${package.Entity}.${entity};
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("<#if package.ModuleName?default("")?trim?length gt 1>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {
</#if>
    private static Logger logger = LoggerFactory.getLogger(${table.controllerName}.class);

    <#assign serviceInstance = table.serviceName?uncap_first> <#-- 创建变量 x -->
    @Autowired
    private ${table.serviceName} ${serviceInstance};

    /**
    * 查询列表
    * @param beanJson
    * @return
    */
    @RequestMapping("/list")
    public BaseResult list(@RequestParam(value = "beanJson", required = false) String beanJson){
        QueryWrapper<${entity}> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(beanJson)){
            ${entity} entity = JSON.parseObject(beanJson, ${entity}.class);
            queryWrapper.setEntity(entity);
        }
        List<${entity}> list = ${serviceInstance}.list(queryWrapper);

        return BaseResult.ok().setData(list);
    }

    /**
    * 查询列表(分页)
    * @param page
    * @param pageSize
    * @return
    */
    @RequestMapping("/page")
    public BaseResult page(@RequestParam(defaultValue = "1") int page,
                         @RequestParam(defaultValue = "10")int pageSize,
                         @RequestParam(value = "beanJson", required = false) String beanJson){
        QueryWrapper<${entity}> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(beanJson)){
          ${entity} entity = JSON.parseObject(beanJson, ${entity}.class);
          queryWrapper.setEntity(entity);
        }
        IPage<${entity}> iPage = new Page<>(page, pageSize);
        iPage= ${serviceInstance}.page(iPage, queryWrapper);

        return BaseResult.ok().setData(iPage);
    }

    /**
    * 获取一个记录
    * @param uuid
    * @return
    */
    @RequestMapping("/selectOne")
    public BaseResult selectOne(String uuid){
        ${entity} entity = ${serviceInstance}.selectOneByUuid(uuid);
        return entity != null ? BaseResult.ok().setData(entity) : BaseResult.fail("记录不存在");
    }

    /**
    * 添加
    * @return
    */
    @RequestMapping("/add")
    @Transactional
    public BaseResult add(@RequestParam("beanJson") String beanJson){
        ${entity} entity = JSON.parseObject(beanJson, ${entity}.class);
        String uuid = MyUtils.uuid();
        entity.setUuid(uuid);
        boolean b = ${serviceInstance}.insert(entity);
        return b ? BaseResult.ok(): BaseResult.fail();
    }

    /**
    * 修改
    * @return
    */
    @RequestMapping("/update")
    @Transactional
    public BaseResult update(@RequestParam("beanJson") String beanJson){
        ${entity} entity = JSON.parseObject(beanJson, ${entity}.class);
        boolean b = ${serviceInstance}.updateByUuid(entity);
        return b ? BaseResult.ok(): BaseResult.fail();
    }

    /**
    * 删除
    * @param uuid 逗号分开
    * @return
    */
    @RequestMapping("/delete")
    @Transactional
    public BaseResult delete(String uuid){
        boolean b = ${serviceInstance}.deleteByUuid(uuid);
        return b ? BaseResult.ok(): BaseResult.fail();
    }
}
</#if>